﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Face;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.Manager
{
    /// <summary>
    /// 用户管理主界面
    /// </summary>
    public partial class FrmSystemUserManagement : Form
    {
        public FrmSystemUserManagement()
        {
            InitializeComponent();
        }

        private void FrmSystemUserManagement_Load(object sender, EventArgs e)
        {
            m_init();
        }

        void m_init()
        {
            this.dgMain.AutoGenerateColumns = false;
            m_bindAllUsers();

        }

        private void m_bindAllUsers()
        {
            UserManageFace face = new UserManageFace();
            List<User> users = face.GetAllUsers();
            this.dgMain.DataSource = users;
            lblUserNum.Text = users.Count.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (new FrmAddUser().ShowDialog() == DialogResult.Yes)
            {
                m_bindAllUsers();
            }          
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgMain.CurrentRow != null && this.dgMain.CurrentRow.DataBoundItem != null)
            {
                User user = (User)this.dgMain.CurrentRow.DataBoundItem;
                FrmEditUser frm = new FrmEditUser(user);
                if (frm.ShowDialog() == DialogResult.Yes)
                {
                    m_bindAllUsers();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgMain.CurrentRow != null && this.dgMain.CurrentRow.DataBoundItem != null)
            {
                if (Tool.ShowQuestionBox("确认要删除此用户吗？") == DialogResult.Yes)
                {
                    User user = (User)this.dgMain.CurrentRow.DataBoundItem;
                    UserManageFace face = new UserManageFace();
                    try
                    {
                        face.DeleteUser(user.Id);
                        m_bindAllUsers();
                        Tool.ShowInformationBox("删除成功!");
                    }
                    catch (Exception)
                    {
                        Tool.ShowInformationBox("删除失败!");
                    }
                }
            }
        }
    }
}
